package com.tom;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.Driver;

/**
 * Servlet implementation class ProcessController
 */
public class ProcessController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private MemberDAO dao;

    /**
     * Default constructor. 
     */
    public ProcessController() {
        super();
        dao = new MemberDAO();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String action = request.getParameter("action");
		
		if (action.equalsIgnoreCase("listmember")){
            List<Member> members = dao.getAll();
            request.setAttribute("members", members);
            RequestDispatcher dispatcher = 
                    request.getRequestDispatcher("listMember.jsp");
            dispatcher.forward(request, response);
    }
    if (action.equalsIgnoreCase("addmember")){
            if (request.getParameter("id")!=null)
            {
                    // save
                    String id = request.getParameter("id");
                    String nickname = request.getParameter("nickname");
                    String password = request.getParameter("password");
                    String email = request.getParameter("email");                   
                    
                    Member m = new Member(id, nickname, password, email, password);
                    
                    HttpSession session = request.getSession();
        			session.setAttribute("m",m);                	                        
                    		
                    if(m.validate())
                    {
                    	dao.add(m);
                                 	
                        List<Member> members = dao.getAll();
                        request.setAttribute("members", members);
                        RequestDispatcher dispatcher = 
                                        request.getRequestDispatcher("success.jsp");
                        dispatcher.forward(request, response);
                    }
                    else
                    {
                        RequestDispatcher dispatcher = 
                                request.getRequestDispatcher("register.jsp");
                        dispatcher.forward(request, response);
                    }
                    
              
            }else
            {
            	RequestDispatcher dispatcher = 
                                    request.getRequestDispatcher("register.jsp");
                dispatcher.forward(request, response);
            }
    }
		
    	/*
		if(request.getParameter("id") != null)
		{
			String id = request.getParameter("id");
			String nickname = request.getParameter("nickname");
			String password = request.getParameter("password");		
			String email = request.getParameter("email");
			
			String commit = request.getParameter("commit");
			
			Connection conn = null;
			PreparedStatement pstmt;
			String sql;
			int rowcount;
			
			Member m = new Member(id, nickname, password, email, commit);
			
			HttpSession session = request.getSession();
			session.setAttribute("m",m);
			
			if(m.validate())
			{
				try 
				{
					DriverManager.registerDriver(new Driver());
					conn = DriverManager.getConnection
							("jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
					sql = "insert into users(id, nickname, password, email) values(?,?,?,?)";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, id);
					pstmt.setString(2, nickname);
					pstmt.setString(3, password);
					pstmt.setString(4, email);			
					
					System.out.println("this is 1 ...");
					
					
					rowcount = pstmt.executeUpdate();
					
					System.out.println("this is 2 ..(" + rowcount + ")");
					
					
					//List<Member> members = (List<Member>) dao.getAll();
					//request.setAttribute("members", members);
					
					//request.getRequestDispatcher("success.jsp").forward(request, response);
																
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					
					response.sendRedirect("register.jsp");
				}
				finally{}
				{
					if(conn != null)
					{
						pstmt = null;
						conn = null;	
					}
					
					System.out.println("this is 3 ...");
					
			
				}
				
				List<Member> members = (List<Member>) dao.getAll();
				
				System.out.println("this is 4 ...(size=" + members.size() + ")");
				
				request.setAttribute("members", members);
				
				System.out.println("this is 5 ...");
				
				request.getRequestDispatcher("success.jsp").forward(request, response);
			}
			else
			{
				response.sendRedirect("register.jsp");
			}
			
		}
		else
		{
			response.sendRedirect("register.jsp");
		}
		*/
		
	}

}
